{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html_str = \"\"\"\n",
    "<body>\n",
    "<div class=\"container\">\n",
    "    <div id=\"first\">\n",
    "        <div class=\"one\">都市</div>\n",
    "        <div class=\"two\">德玛西亚</div>\n",
    "        <div class=\"two three\">王牌对王牌</div>\n",
    "        <a>\n",
    "            <div class=\"spe\">特殊位置</div>\n",
    "        </a>\n",
    "    </div>\n",
    "    <div id=\"second\">\n",
    "        <div class=\"three four\">水电费</div>\n",
    "        <div class=\"three\">说的话房间不开封</div>\n",
    "        <div class=\"four\">三顿饭黑客技术</div>\n",
    "    </div>\n",
    "    <div id=\"third\">\n",
    "        <div class=\"three six\">水电费</div>\n",
    "        <div class=\"three seven\">说的话房间开封</div>\n",
    "    </div>\n",
    "</div>\n",
    "</body>\n",
    "\"\"\"\n",
    "\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['first', 'third']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@id, \"i\")]/@id'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 与"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['two three', 'three four', 'three', 'three six', 'three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"three\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['three six']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"three\") and contains(@class, \"six\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['three', 'three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"three\") and contains(text(), \"房间\")]/@class'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 或"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"seven\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['two', 'three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"seven\") or contains(text(), \"德玛西亚\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['two three', 'three four', 'three', 'three six', 'three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"seven\") or contains(@class, \"three\")]/@class'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 非"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7ff61bd79b40>, <Element div at 0x7ff61bdb6280>, <Element div at 0x7ff61bdb6370>, <Element div at 0x7ff61bdb6320>, <Element div at 0x7ff61bdb6460>, <Element div at 0x7ff61bdb64b0>, <Element div at 0x7ff61bdb6500>, <Element div at 0x7ff61bdb6550>, <Element div at 0x7ff61bdb65a0>, <Element div at 0x7ff61bdb65f0>, <Element div at 0x7ff61bdb6640>, <Element div at 0x7ff61bdb6690>, <Element div at 0x7ff61bdb66e0>] 13\n"
     ]
    }
   ],
   "source": [
    "divs = html.xpath('.//div')\n",
    "print(divs, len(divs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['container', 'one', 'two', 'two three', 'spe', 'three four', 'three', 'four', 'three six', 'three seven'] 10\n"
     ]
    }
   ],
   "source": [
    "divs = html.xpath('.//div[not(@id)]/@class')\n",
    "print(divs, len(divs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element div at 0x7ff61bd79b40>, <Element div at 0x7ff61bdb6dc0>, <Element div at 0x7ff61bdb6370>, <Element div at 0x7ff61bdb6320>, <Element div at 0x7ff61bdb6460>, <Element div at 0x7ff61bdb64b0>, <Element div at 0x7ff61bdb6e10>, <Element div at 0x7ff61bdb6550>, <Element div at 0x7ff61bdb65a0>, <Element div at 0x7ff61bdb65f0>, <Element div at 0x7ff61bdb6690>, <Element div at 0x7ff61bdb66e0>] 12\n"
     ]
    }
   ],
   "source": [
    "divs = html.xpath('.//div[not(@id=\"third\")]')\n",
    "print(divs, len(divs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['container', 'one', 'two', 'two three', 'spe', 'three four', 'three', 'four', 'three six', 'three seven'] 10\n"
     ]
    }
   ],
   "source": [
    "divs = html.xpath('.//div[not(@id=\"third\")]/@class')\n",
    "print(divs, len(divs))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 混用与或非是不推荐的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['three', 'three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"three\") and contains(text(), \"房间\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['two', 'two three', 'three', 'three seven']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//div[contains(@class, \"three\") and contains(text(), \"房间\") or contains(@class, \"two\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
